# coding: UTF-8
# Filename : Mysql.py
# Company : imedia
# Author : rose
# -*-coding:utf-8 -*-

import ConfigParser
import MySQLdb
import sys,os
import traceback
import datetime
import threading
import log,utils
reload(sys) 
sys.setdefaultencoding('utf-8')

#获取当前执行文件的绝对路径
url = utils.readFromIni('url').strip()
database = utils.readFromIni('database').strip()
username = utils.readFromIni('username').strip()
password = utils.readFromIni('password').strip()
charset = utils.readFromIni('charset').strip()


#获取连接
def getConnection():
    conn = None
    try:
        conn = MySQLdb.Connection(url, username, password, database, use_unicode=True,charset='utf8')
    except:
        log.print_error_log('exception:mysql->method_getConnection_获取连接异常' + traceback.format_exc())
    return conn

#执行插入脚本
#sql: 插入语句
#param: 为参数['', '']
def insert(sql, param):
    if sql == None or sql.strip() == '':
        log.print_error_log('warning:mysql.py->method_insert_插入语句为空')
        return
    result = 0
    try:
        conn = getConnection()
        curs = conn.cursor()
        result = curs.execute(sql, param)
    except:
        conn.rollback()
        log.print_error_log('exception:mysql.py->method_insert_数据插入异常' + traceback.format_exc())
    finally:
        conn.commit()
        curs.close()
        conn.close()
    if result > 0:
        return True
    else:
        return False    
#执行批量插入脚本
#sql: 插入语句
#param: 为参数[(),(),()]
def insertMany(sql, param):
    if sql == None or sql.strip() == '':
        log.print_error_log('warning:mysql.py->method_insert_插入语句为空')
        return
    result = 0
    try:
        conn = getConnection()
        curs = conn.cursor()
        #执行批量插入
        result = curs.executemany(sql, param)
    except:
        conn.rollback()
        log.print_error_log('exception:mysql.py->method_insertMany_批量数据插入异常' + traceback.format_exc())
    finally:
        conn.commit()
        curs.close()
        conn.close()
    if result > 0 :
        return True
    else:
        return False
#查找某表数据
def doSearch(sql, param):
    if sql == None or sql.strip() == '':
        log.print_error_log('warning:mysql.py->method_doSearch_查询语句为空')
        return
    cds = []
    try:
        conn = getConnection()
        curs = conn.cursor()
        curs.execute(sql, param)
        cds = curs.fetchall()
    except:
        log.print_error_log('exception:mysql.py->method_doSearch_查询异常' + traceback.format_exc())
    finally:
        curs.close()
        conn.close()
    return cds
#查找某条表数据
def get(sql, param):
    if sql == None or sql.strip() == '':
        log.print_error_log('warning:mysql.py->method_get_查询语句为空')
        return
    cds = []
    try:
        conn = getConnection()
        curs = conn.cursor()
        curs.execute(sql, param)
        cds = curs.fetchone()
    except:
        log.print_error_log('exception:mysql.py->method_get_查询异常' + traceback.format_exc())
    finally:
        curs.close()
        conn.close()
    return cds
#执行删除
def delete(sql):
    if sql == None or sql.strip() == '':
        log.print_error_log('warning:mysql.py->method_delete_删除语句为空')
        return
    result = 0
    try:
        conn = getConnection()
        curs = conn.cursor()
        result = curs.execute(sql)
    except:
        conn.rollback()
        log.print_error_log('error:mysql.py->method_delete_删除出现异常' + traceback.format_exc())
    finally:
        conn.commit()
        curs.close()
        conn.close()
    if result > 0:
        return True
    else:
        return False

def escape(string):
    return MySQLdb.escape_string(string)
